package cn.lwj.common.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.log4j.Logger;


public class InterceptFilter implements Filter{

	public static List<String> noauthList = new ArrayList<String>();

	private Logger log=Logger.getLogger(this.getClass());

	public void init(FilterConfig arg0) throws ServletException {
		noauthList.add("/jsp/admin/");
		noauthList.add("/error/sso_500.jsp");
		noauthList.add("/nagios_monitor.jsp");
		noauthList.add("/nagios_test.jsp");
		noauthList.add("/ysdjController/");
	}

	public void destroy() {}

    public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
        arg2.doFilter(arg0, arg1);
    }

	/*public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
		String url = request.getRequestURI();

		//验证用户登录与否
		HttpSession  session = request.getSession();

		//判断是否需要进行登陆验证
		boolean thisUrlNoAuthor=false;

		for(String noauthUrl:noauthList){ //排除不需要验证登陆的Url
			if(url.indexOf(noauthUrl)!=-1){
				thisUrlNoAuthor=true;
			}
		}

		//单点登录
		if((url.endsWith("sso"))){
			String ssoUrl;
			try {
				ssoUrl = SSOUtil.getSSOLoginUrl(null);
				response.sendRedirect(ssoUrl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if(thisUrlNoAuthor){
			arg2.doFilter(arg0, arg1);
		}else if(session.getAttribute(BaseControl.LOGIN_LOGINSESSION_KEY)==null&&(url.endsWith("jsp"))){
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache, must-revalidate");
			response.setHeader("Pragma", "no-cache");
			response.sendRedirect(request.getContextPath()+"/sso");
		}else{
			arg2.doFilter(arg0, arg1);
		}
	}*/
}
